package com.code.platform.common.open;

import com.code.platform.common.http.HttpClientUtil;
import com.code.platform.common.mapper.JsonMapper;
import com.code.platform.common.utils.StringUtils;
import com.fasterxml.jackson.databind.JavaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component("OpenServiceClient")
public class OpenServiceClient {
    private static final Logger logger = LoggerFactory.getLogger(OpenServiceClient.class);

    public final static String CHARSET = "UTF-8";

    /**
     * 调用开放平台服务
     *
     * @param url    服务URL
     * @param params 服务参数
     * @return 含有 isSuccess、response、errorMessage 三项信息的 Map，其中的 response 内容需要参考具体的服务
     */
    public static Map post(String url, Map<String, String> params) {
        if (logger.isDebugEnabled())
            logger.debug("调用开放平台服务：{}", url);

        String resStr = HttpClientUtil.post(url, params, CHARSET, CHARSET);
        if (StringUtils.isBlank(resStr)) {
            return new HashMap();
        }

        return JsonMapper.getInstance().fromJson(resStr, Map.class);
    }

    public static Map get(String url) {
        String resStr = HttpClientUtil.get(url);
        if (StringUtils.isBlank(resStr)) {
            return new HashMap();
        }

        return JsonMapper.getInstance().fromJson(resStr, Map.class);
    }


    /**
     * 判定调用服务是否成功
     *
     * @param retMap 调用服务返回的 Map
     * @return 成功：true；失败：false
     */
    public static boolean isSuccess(Map retMap) {
        if (retMap == null)
            return false;

        boolean isSuccess = false;
        if (retMap.get("isSuccess") != null)
            isSuccess = Boolean.parseBoolean((String) retMap.get("isSuccess"));
        return isSuccess;
    }

    /**
     * 获取错误信息
     *
     * @param retMap 调用服务返回的 Map
     * @return 错误信息，无错误信息返回空串
     */
    public static String getErrorMessage(Map retMap) {
        if (retMap == null)
            return "";
        String errorMessage = "";
        if (retMap.get("errorMessage") != null)
            errorMessage = (String) retMap.get("errorMessage");
        return errorMessage != null ? errorMessage : "";
    }

    /**
     * 获取错误信息
     *
     * @param retMap 调用服务返回的 Map
     * @return 错误信息，无错误信息返回空串
     */
    public static String getMessage(Map retMap) {
        if (retMap == null)
            return "";
        String message = "";
        if (retMap.get("response") != null)
            message = (String) retMap.get("response");
        return message != null ? message : "";
    }

    /**
     * 获取响应信息
     *
     * @param retMap 调用服务返回的 Map
     * @return Map 需要根据具体的服务从其中获取信息
     */
    public static Map getResponse(Map retMap) {
        if (retMap == null)
            return new HashMap();
        Map response = null;
        if (retMap.get("response") != null && !"".equals(retMap.get("response")))
            response = (Map) retMap.get("response");
        return response != null ? response : new HashMap();
    }

    public static String getResponseAsString(Map retMap) {
        if (retMap == null)
            return null;
        if (retMap.get("response") != null && !"".equals(retMap.get("response"))) {
            return JsonMapper.getInstance().toJson(retMap.get("response"));
        }
        return null;
    }

    public static <T> T getResponseAsObject(Map retMap, Class<T> clazz) {
        String response = getResponseAsString(retMap);
        if (!StringUtils.isEmpty(response)) {
            return JsonMapper.getInstance().fromJson(response, clazz);
        }
        return null;
    }

    public static <T> List<T> getResponseAsArray(Map retMap, Class<T> clazz) {
        String response = getResponseAsString(retMap);
        if (!StringUtils.isEmpty(response)) {
            JavaType javaType = JsonMapper.getInstance().createCollectionType(ArrayList.class, clazz);
            return JsonMapper.getInstance().fromJson(response, javaType);
        }
        return null;
    }

    public static <T> List<T> getResponseAsArray( String response, Class<T> clazz) {
        if (!StringUtils.isEmpty(response)) {
            JavaType javaType = JsonMapper.getInstance().createCollectionType(ArrayList.class, clazz);
            return JsonMapper.getInstance().fromJson(response, javaType);
        }
        return null;
    }

    public static <T> T getResponseAsObject(Map retMap, JavaType javaType) {
        String response = getResponseAsString(retMap);
        if (!StringUtils.isEmpty(response)) {
            return JsonMapper.getInstance().fromJson(response, javaType);
        }
        return null;
    }

    /**
     * 获取响应信息
     *
     * @param retMap 调用服务返回的 Map
     * @return Map 需要根据具体的服务从其中获取信息
     */
    public static List getResponseAsList(Map retMap) {
        if (retMap == null)
            return new ArrayList();
        List response = null;
        if (retMap.get("response") != null && !"".equals(retMap.get("response")))
            response = (List) retMap.get("response");
        return response != null ? response : new ArrayList();
    }


}
